function DetectAreas(fvideo, fin, fout, fsubtract, fareas, handles)

load(fareas);

if isempty(selecttypesfilefull)
    selecttypesfilefull = 'selecttypes.mat';
end

load(selecttypesfilefull);

v = mmreader(fvideo);

load(fin);

load(fsubtract);
cc = [cr1, cr2, cg1, cg2, cb1, cb2, ch1, ch2, cs1, cs2, cv1, cv2];

templatelen = length(imgtypes);


len = length(objs);

areasx1 = zeros(len, nfindobjs, numberoftypes);
areasx2 = zeros(len, nfindobjs, numberoftypes);
areasy1 = zeros(len, nfindobjs, numberoftypes);
areasy2 = zeros(len, nfindobjs, numberoftypes);
areasx0 = zeros(len, nfindobjs, numberoftypes);
areasy0 = zeros(len, nfindobjs, numberoftypes);
areascores = -ones(len, nfindobjs, numberoftypes);
areacdists = -ones(len, nfindobjs, numberoftypes);

bestscores = ones(len, numberoftypes)*10000;
bestscoreindexes = zeros(len, numberoftypes);

bwarnings = zeros(len, nfindobjs, numberoftypes);


detecttypes = zeros(numberoftypes, 1);

lentypes = length(imgtypes);

%It may be possible to detect different areas such as male area, 
%female area, beak, but the user has not selected all of these for
% detection (for example only selects male area and beak). So check 
%which ones have been assigned for detection (this is given by the
%imgtypes array which is created when the user calibrates the 'detect
%areas')
for i = 1:lentypes
    detecttypes(imgtypes(i)) = 1;
end

n = 0;
stepcount = 1;

for i = 1:len 
       
    vindex = i + nprocessstart - 1;
    
    img = read(v,vindex);    
       
        for j = 1:nfindobjs           
        
            x1 = x1s(i, j); 
            x2 = x2s(i, j); 
            y1 = y1s(i, j); 
            y2 = y2s(i, j);             
        
            if x1 > 0 & x2 > 0 & y1 > 0 & y2 > 0                 
                objectimg = img(x1:x2, y1:y2, :);     
            
                n = n + 1; 
    
                for k = 1:numberoftypes

                    if detecttypes(k) == 1
            
                        bestscore = 0;
            
                        bestx1 = 0; 
                        bestx2 = 0;
                        besty1 = 0;
                        besty2 = 0;     
                                                
            
                        %contains the number of objects selected for each
                        %type (e.g beak, female area, ...). Check that the 
                        %user has selected at least one area as a template
                        %during calibration (this should not occur if 
                        %detecttypes(k) == 1 however).
                        if nobjecttypecounts(k) > 0 
                            ntype = k;
                                                            
                            [bestx1, bestx2, besty1, besty2, bestscore, bestindex, bestcdist] = TempMatchArea(objectimg, fullimgs, imgtypes, ntype, templatelen, rimgmeans, gimgmeans, bimgmeans, rimgstds, gimgstds, bimgstds, cc);                                            
                        end                    
            
                        if bestx1 > 0
                        
                            areasx1(i, j, k) = bestx1;
                            areasx2(i, j, k) = bestx2;
                            areasy1(i, j, k) = besty1;
                            areasy2(i, j, k) = besty2;
                        
                            areasx0(i, j, k) = round((bestx2-bestx1)/2.0)+bestx1;
                            areasy0(i, j, k) = round((besty2-besty1)/2.0)+besty1;
            
                            areascores(i, j, k) = bestscore;
                            areacdists(i, j, k) = bestcdist;
                        
                            if bestscore < bestscores(i,k)
                                bestscores(i,k) = bestscore;
                                bestscoreindexes(i,k) = j;
                            end
                                             
                        end
                                                        
                    end
            
                end    
                
            end
                       
        end
    
    stepcount = stepcount + 1;
    p = floor(100*(stepcount/(len+1)));
    s = strcat('Area Detection...', num2str(p));
    s = strcat(s, '%');    
    set(handles.textoutput, 'String', s);    
end


scoremeans = zeros(numberoftypes, nfindobjs);
scorestds = zeros(numberoftypes, nfindobjs);
cdistmeans = zeros(numberoftypes, nfindobjs);
cdiststds = zeros(numberoftypes, nfindobjs);

dminscores = zeros(numberoftypes, nfindobjs);
dmincdists = zeros(numberoftypes, nfindobjs);

n = 2;
c = 0.5;

if n > 1
    
    for i = 1:numberoftypes  
        for j = 1:nfindobjs
            x = areascores(:,j,i);
            y = areacdists(:,j,i);
            
            x = x(:);
            y = y(:);
            
            a = find(x > -1);
            b = find(y > -1);
            
            if~isempty(a) & ~isempty(b)
                scoremeans(i,j) = mean(x(a));
                scorestds(i,j) = std(x(a));   
        
                cdistmeans(i,j) = mean(y(b));
                cdiststds(i,j) = std(y(b));   
                                
                dminscore = scoremeans(i,j)-c*scorestds(i,j);
                dmincdist = cdistmeans(i,j)-c*cdiststds(i,j); 
                
                dminscores(i,j) = dminscore;
                dmincdists(i,j) = dmincdist;

                a = find(areascores(:, j, i) > dminscore); 
                bwarnings(a, j, i) = 1;
            end
            
        end
    end           
end


save(fout, 'objs', 'x1s', 'x2s', 'y1s', 'y2s', 'ex0s', 'ey0s', 'ephis', 'eas', 'ebs', 'ets', ...
    'xheights', 'yheights', 'areas', 'nprocessstart', 'imgx', 'imgy', 'imghalfx', 'imghalfy', ...
    'areasx1', 'areasx2', 'areasy1', 'areasy2', 'areasx0', 'areasy0', 'areacdists', 'areascores',...
    'nfindobjs', 'nfindmaxobjs', 'nstartframe', 'nendframe', ...
    'cdistmeans', 'cdiststds', 'scoremeans', 'scorestds', 'bestscores', 'bestscoreindexes',...
    'bwarnings', 'detecttypes', 'selecttypesfilefull', 'objs1', 'bsegmented', 'dminscores', 'dmincdists');



function [bestx1, bestx2, besty1, besty2, bestscore, bestindex, besthdist] = TempMatchArea(objectimg, fullimgs, imgtypes,...
    ntype, len, rmeans, gmeans, bmeans, rstds, gstds, bstds, cc)

dobjectimg = double(objectimg);

bestindex = 0;
besthdist = 100000;
bestscore = 100000;
bestx1 = 0;
bestx2 = 0;
besty1 = 0;
besty2 = 0;

minsize = 0.5;

objsize = size(objectimg);

objarea = objsize(1)*objsize(2);

for i = 1:len  %number of templates               
    if imgtypes(i) == ntype
        
        templateimg = cell2mat(fullimgs(i)); 
                
        sz = size(templateimg);
        templatearea = sz(1)*sz(2);
        
        psize = templatearea/objarea;

        if psize < minsize
                    
            dtemplateimg = double(templateimg);
        
            k = ntype;

            [x1, x2, y1, y2, score, nremoved, hdist] = TempMatchAreaC(dtemplateimg, dobjectimg, rmeans(i), gmeans(i),...
                bmeans(i), rstds(i), gstds(i), bstds(i));            
            
            pscore = score/templatearea;
            
            if pscore < bestscore   
                besthdist = hdist;
                bestscore = pscore;
                bestindex = i;
            
                bestx1 = x1;
                bestx2 = x2;
                besty1 = y1;
                besty2 = y2;
            end
        end
    end
end







        